package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.constant.*;
import cn.com.cifi.mars.entity.ProjectLockRecord;
import cn.com.cifi.mars.mapper.mars.ProjectLockRecordMapper;
import cn.com.cifi.mars.service.ProjectLockRecordService;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.util.UUIDUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 明源定调价项目锁定服务实现类
 */
@Service
public class ProjectLockRecordServiceImpl extends ServiceImpl<ProjectLockRecordMapper, ProjectLockRecord> implements ProjectLockRecordService {

    @Autowired
    private ProjectLockRecordMapper projectLockRecordMapper;
    @Autowired
    private SysLogService sysLogService;

    /**
     * 添加项目锁定记录
     * @param projectId 项目id
     * @param schemeId 方案id
     */
    @Override
    public void add(String projectId, String schemeId,String lockDetails) {
        LocalDateTime now = LocalDateTime.now();
        //根据项目id、方案id查询未解锁记录
        ProjectLockRecord projectLockRecord = new ProjectLockRecord();
        projectLockRecord.setId(UUIDUtils.create());
        projectLockRecord.setProjectId(projectId);
        projectLockRecord.setSchemeId(schemeId);
        projectLockRecord.setIsLock(WhetherEnum.YES.getKey());
        projectLockRecord.setLockTime(now);
        projectLockRecord.setLockDetails(lockDetails);
        projectLockRecord.setCreateTime(now);
        projectLockRecordMapper.insert(projectLockRecord);
        saveLog(LogEventEnum.PRESERVATION,"ESB消息接收","明源定调价添加项目锁定记录",projectLockRecord.toString());
    }

    /**
     * 解锁项目
     * @param projectId 项目id
     * @param projectCode 项目code
     * @param schemeId 方案id
     * @param unlockDetails 解锁
     */
    public String update(String projectId, String projectCode, String schemeId, String unlockDetails){
        //根据项目id、方案id查询未解锁记录
        List<ProjectLockRecord> projectLockRecordList  = projectLockRecordMapper.selectByProjectIdAndSchemeId(projectId,schemeId);
        if(CollectionUtils.isNotEmpty(projectLockRecordList)){
            for(ProjectLockRecord projectLockRecord : projectLockRecordList){
                projectLockRecord.setIsLock(WhetherEnum.NO.getKey());
                projectLockRecord.setUnlockTime(LocalDateTime.now());
                projectLockRecord.setUnlockDetails(unlockDetails);
                projectLockRecordMapper.updateById(projectLockRecord);
            }
            return "解锁成功";
        }
        return null;
    }

    /**
     * 查询项目是否锁定
     * @param projectId 项目id
     * @return
     */
    @Override
    public boolean isLockByProjectId(String projectId){
        ProjectLockRecord projectLockRecord = projectLockRecordMapper.isLockByProjectId(projectId);
        if(null == projectLockRecord  || projectLockRecord.getIsLock() == 0){
            return false;
        }
        return true;
    }

    @Override
    public boolean isLockBySchemeId(String schemeId) {
        QueryWrapper<ProjectLockRecord> queryWrapper = new QueryWrapper<ProjectLockRecord>();
        queryWrapper.eq("scheme_id", schemeId);
        ProjectLockRecord projectLockRecord = projectLockRecordMapper.selectOne(queryWrapper);
        //是否锁定，如果锁定，返回true
        if (projectLockRecord != null){
            int isLock = projectLockRecord.getIsLock();
            if (isLock == 1){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


    /**
     * 保存系统日志
     */
    private boolean saveLog(LogEventEnum logEventEnum, String module, String title, String content) {
        SysLogBO bo = new SysLogBO();
        bo.setEvent(logEventEnum);
        bo.setSource(LogSrcEnum.SYS);
        bo.setModule(module);
        bo.setTitle(title);
        bo.setContent(content);
        bo.setType(LogTypeEnum.ONE.getKey());
        bo.setIsSuccess(IsSuccessEnum.SUCCESS.getKey());
        return sysLogService.add(bo,Constants.SYS);
    }
}
